/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import MyBean.Connessione;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author dav
 */
public class cRicerca extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    Connessione con = new Connessione();
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        char action = request.getParameter("action").toCharArray()[0];
        switch(action){
            case 'p':ricercaP(request,response);break;
            case 'm':ricercaM(request,response);break;
            case 'r':ricercaR(request,response);break;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(cRicerca.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(cRicerca.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void ricercaP(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        PrintWriter out = response.getWriter();
        java.sql.Statement st = con.getConn();
        String titolo = request.getParameter("titolo");
        String autore = request.getParameter("autore");
        String editore = request.getParameter("editore");
        String anno = request.getParameter("anno");
        ResultSet rs = null;
        if(anno.equals("")){
                rs = 
                st.executeQuery("SELECT * FROM libro WHERE titolo LIKE '%"+
                titolo+"%'"+"AND autore LIKE '%"+
                autore+"%'"+"AND editore LIKE '%"+
                editore+"%'");
        }else{
                int annno=Integer.valueOf(anno).intValue();
                rs = 
                st.executeQuery("SELECT * FROM libro WHERE titolo LIKE '%"+
                titolo+"%'"+"AND autore LIKE '%"+
                autore+"%'"+"AND editore LIKE '%"+
                editore+"%'"+"AND anno = "+annno);
        }
        out.println("<table id=\"res_table\"><th>Titolo</th><th>Autore</th><th>Editore</th><th>Anno</th><th>Prenota</th>");
        int i=0;
        while(rs.next()){
                if(i%2==0){
                    out.println("<tr class=\"scuro\"><td>");
                }
                else{
                    out.println("<tr class=\"chiaro\"><td>");
                }                
                        out.println(rs.getString("Titolo"));
                        out.println("</td><td>");
                        
                        out.println(rs.getString("Autore"));
                        out.println("</td><td>");

                        out.println(rs.getString("Editore"));
                        out.println("</td><td>");

                        out.println(rs.getString("Anno"));                        
                        out.println("</td><td><form action=\"prenota.jsp\" method=\"POST\">"
                                + "<input type=\"hidden\" name=\"titololibro\" value=\""
                                + rs.getString("Titolo")
                                + "\">"
                                + "<input type=\"hidden\" name=\"autorelibro\" value=\""
                                + rs.getString("Autore")
                                + "\">"
                                + "<input type=\"hidden\" name=\"editorelibro\" value=\""
                                + rs.getString("Editore")
                                + "\">"
                                + "<input type=\"hidden\" name=\"annolibro\" value=\""
                                + rs.getString("Anno")
                                + "\">"
                                + "<input type=\"submit\" value=\"prenota\">");
                        out.println("</form></td></tr>");
                        
                        i++;
                    }
        
        out.println("</table>");
    }

    private void ricercaM(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        PrintWriter out = response.getWriter();
        java.sql.Statement st = con.getConn();
        String titolo = request.getParameter("titolo");
        String autore = request.getParameter("autore");
        String editore = request.getParameter("editore");
        String anno = request.getParameter("anno");
        ResultSet rs = null;
        if(anno.equals("")){
                rs = 
                st.executeQuery("SELECT * FROM libro WHERE titolo LIKE '%"+
                titolo+"%'"+"AND autore LIKE '%"+
                autore+"%'"+"AND editore LIKE '%"+
                editore+"%'");
        }else{
                int annno=Integer.valueOf(anno).intValue();
                rs = 
                st.executeQuery("SELECT * FROM libro WHERE titolo LIKE '%"+
                titolo+"%'"+"AND autore LIKE '%"+
                autore+"%'"+"AND editore LIKE '%"+
                editore+"%'"+"AND anno = "+annno);
        }
        out.println("<table id=\"res_table\"><th>Titolo</th><th>Autore</th><th>Editore</th><th>Anno</th><th>Gestisci</th>");
        int i=0;
        while(rs.next()){
                if(i%2==0){
                    out.println("<tr class=\"scuro\"><td>");
                }
                else{
                    out.println("<tr class=\"chiaro\"><td>");
                }
                        out.println(rs.getString("Titolo"));
                        out.println("</td><td>");
                        
                        out.println(rs.getString("Autore"));
                        out.println("</td><td>");

                        out.println(rs.getString("Editore"));
                        out.println("</td><td>");

                        out.println(rs.getString("Anno"));
                        out.print("</td><td><form action=\"modifica.jsp\" method=\"POST\" style=\"display:inline\">"
                                + "<input type=\"hidden\" name=\"titololibro\" value=\""
                                + rs.getString("Titolo")
                                + "\">"
                                + "<input type=\"hidden\" name=\"autorelibro\" value=\""
                                + rs.getString("Autore")
                                + "\">"
                                + "<input type=\"hidden\" name=\"editorelibro\" value=\""
                                + rs.getString("Editore")
                                + "\">"
                                + "<input type=\"hidden\" name=\"annolibro\" value=\""
                                + rs.getString("Anno")
                                + "\">"
                                + "<input type=\"submit\" value=\"modifica\"></form>");
                        out.print("<form action=\"cGestore?action=e\" method=\"POST\">"
                                + "<input type=\"hidden\" name=\"titololibro\" value=\""
                                + rs.getString("Titolo")
                                + "\">"
                                + "<input type=\"hidden\" name=\"autorelibro\" value=\""
                                + rs.getString("Autore")
                                + "\">"
                                + "<input type=\"hidden\" name=\"editorelibro\" value=\""
                                + rs.getString("Editore")
                                + "\">"
                                + "<input type=\"hidden\" name=\"annolibro\" value=\""
                                + rs.getString("Anno")
                                + "\">"
                                + "<input type=\"submit\" value=\"cancella\">");
                        out.println("</td></tr>");
                        
                        i++;
                    }
        
        out.println("</table>");
    
    }

    private void ricercaR(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        PrintWriter out = response.getWriter();
        java.sql.Statement st = con.getConn();
        String titolo = request.getParameter("titolo");
        String autore = request.getParameter("autore");
        String editore = request.getParameter("editore");
        String anno = request.getParameter("anno");
        ResultSet rs = null;
        if(anno.equals("")){
                rs = 
                st.executeQuery("SELECT * FROM libro WHERE titolo LIKE '%"+
                titolo+"%'"+"AND autore LIKE '%"+
                autore+"%'"+"AND editore LIKE '%"+
                editore+"%'");
        }else{
                int annno=Integer.valueOf(anno).intValue();
                rs = 
                st.executeQuery("SELECT * FROM libro WHERE titolo LIKE '%"+
                titolo+"%'"+"AND autore LIKE '%"+
                autore+"%'"+"AND editore LIKE '%"+
                editore+"%'"+"AND anno = "+annno);
        }
        out.println("<table id=\"res_table\"><th>Titolo</th><th>Autore</th><th>Editore</th><th>Anno</th>");
        int i=0;
        while(rs.next()){
                if(i%2==0){
                    out.println("<tr class=\"scuro\"><td>");
                }
                else{
                    out.println("<tr class=\"chiaro\"><td>");
                }
                        out.println(rs.getString("Titolo"));
                        out.println("</td><td>");
                        
                        out.println(rs.getString("Autore"));
                        out.println("</td><td>");

                        out.println(rs.getString("Editore"));
                        out.println("</td><td>");

                        out.println(rs.getString("Anno"));
                        out.println("</td></tr>");
                        
                        i++;
                    }
        
        out.println("</table>");
    }    
}

